<template>
  <div class="home-container">
    <!-- 已登录 -->
    <div
      class="header userInfo"
      v-if="userInfo"
    >
      <div class="baseInfo">
        <div class="left">
          <van-image
            width="100"
            height="100"
            round
            :src="userInfoD.photo"
            class="avatar"
          />
          <span class="name">{{userInfoD.name}}</span>
        </div>
        <div class="right">
          <van-button
            type="default"
            size="mini"
            round
            to="/user/profile"
          >编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span>{{userInfoD.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfoD.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfoD.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfoD.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
      <van-grid :column-num="2">
        <van-grid-item
          text="收藏"
          class="gridItem"
        >
          <template #icon><i class="toutiao toutiao-shoucang"></i></template>
          <template #text><span class="text">收藏</span></template>
        </van-grid-item>
        <van-grid-item
          text="文字"
          class="gridItem"
        >
          <template #icon><i class="toutiao toutiao-lishi"></i></template>
          <template #text><span class="text">历史</span></template>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 未登录 -->
    <div
      class="header not-login"
      v-else
    >
      <img
        src="~@/assets/mobile.png"
        alt=""
        class="pic"
      >
      <span
        class="text"
        @click="$router.push('/login')"
      >登录 / 注册</span>
    </div>

    <!-- 宫格 -->
    <!-- <van-grid :column-num="2">
      <van-grid-item
        text="收藏"
        class="gridItem"
      >
        <template #icon><i class="toutiao toutiao-shoucang"></i></template>
        <template #text><span class="text">收藏</span></template>
      </van-grid-item>
      <van-grid-item
        text="文字"
        class="gridItem"
      >
        <template #icon><i class="toutiao toutiao-lishi"></i></template>
        <template #text><span class="text">历史</span></template>
      </van-grid-item>
    </van-grid> -->
    <!-- 单元格导航 -->
    <van-cell
      title="消息通知"
      is-link
    />
    <van-cell
      title="小智同学"
      is-link
    />
    <van-cell
      v-if="userInfo"
      class="logout-cell"
      title="退出登录"
      center
      clickable
      @click="logout"
    />
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'myPage',
  components: {
  },
  props: {},
  data () {
    return {
      userInfoD: {}
    }
  },
  computed: {
    // vuex内部的user 数据
    // 存在 登录
    // 不存在 未登录
    userInfo () {
      return this.$store.state.user
    }
  },
  watch: {},
  created () {
    // 如果登录了才可以获取到用户信息
    if (this.userInfo) {
      this.loadUser()
    }
  },
  mounted () {
    console.log(this.userInfo)
  },
  methods: {
    logout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出吗？'
      })
        .then(() => {
          // on confirm
          console.log('确认')
          // 清除token
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          console.log('取消')
        })
    },
    async loadUser () {
      console.log(111)
      try {
        const { data } = await getUserInfo()
        console.log(data.data)
        this.userInfoD = data.data
        console.log('userInfoD')
      } catch (err) {
        console.log(err)
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  > .header {
    // height: 361px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    flex-direction: column;
    &.not-login {
      height: 361px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .pic {
      width: 132px;
      height: 132px;
      margin-bottom: 25px;
    }
    .text {
      color: #fff;
      font-size: 28px;
    }
  }
  .userInfo {
    .baseInfo {
      height: 231px;
      padding: 76px 32px 23px;
      // background: #ccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      margin-bottom: 6px;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }
    .data-stats {
      display: flex;
      justify-content: space-around;
      .data-item {
        height: 130px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  .gridItem {
    background: #fff;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
